package com.itheima.chapter03.controller;

import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;

@Controller
public class CustomErrorController implements ErrorController {

    @RequestMapping("/error")
    public String handleError(HttpServletRequest request, Model model) {
        Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
        String requestUri = request.getRequestURI();

        if (status != null) {
            Integer statusCode = Integer.valueOf(status.toString());

            if ("/dish/deli/0".equals(requestUri)) {
                model.addAttribute("error", "中午");
            } else if (statusCode == 404) {
                model.addAttribute("error", "页面未找到");
            } else if (statusCode == 500) {
                model.addAttribute("error", "内部服务器错误");
            } else if (statusCode == 400) {
                model.addAttribute("error", "错误的请求");
            } else {
                model.addAttribute("error", "未知错误");
            }
        }

        return "error";
    }
}